package edu.uwlax.cs.oayonlinestore.client.utils;

import com.google.gwt.user.client.ui.SimplePanel;

public abstract class StackPage extends SimplePanel {

	private boolean isInited = false;

	private StackPanel parentPanel;

	public void callOnShow(String param) {
		if (param == null || param.trim().length() == 0)
			onShow();
		else {
			boolean s, i = false;
			try {
				i = onShow(Integer.parseInt(param));
			} catch (NumberFormatException e) {
			}
			s = onShow(param);
			if (!s && !i)
				throw new RuntimeException(
						"This page does not accept the parameter " + param);
		}
	}

	protected abstract void create();

	public void doInit() {
		if (isInited)
			return;
		preCreate();
		create();
		isInited = true;
	}

	public String getAnchor() {
		String s = parentPanel.getParentPath();
		return (s.length() == 0) ? getID() : s + "-" + getID();
	}

	public String getAnchor(int param) {
		return getAnchor() + "-" + param;
	}

	public String getAnchor(String param) {
		return getAnchor() + "-" + param;
	}

	public abstract String getID();

	public StackPanel getParentPanel() {
		return parentPanel;
	}

	public boolean onHide() {
		return true;
	}

	protected void onShow() {
	}

	protected boolean onShow(int param) {
		return false;
	}

	protected boolean onShow(String param) {
		return false;
	}

	protected void preCreate() {
	}

	public void setParentPanel(StackPanel parentPanel) {
		this.parentPanel = parentPanel;
	}
}
